/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  LogHeadHelper.java                                      
 *  File Contents Outline :  日志头持久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbLogHead;

import storage.util.StorageManager;


public class LogHeadHelper {

    /**
     * 将logHead保存到LaUDMS数据库中
     *
     * @param logHead 要存储的模型对象
     */
    public void saveLogHead(DbLogHead logHead) {
      //  StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_logHead);
                                PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logHead"));
//        sm.open();
        sm.setRowKey(logHead.getRowKey());
        sm.put(DbLogHead.cl_name, logHead.getName());
        sm.put(DbLogHead.cl_location, logHead.getLocation());
        sm.put(DbLogHead.cl_chinese, logHead.getChinese());
//        sm.close();

    }

    /**
     * 将一系列logHead保存到LaUDMS数据库中
     *
     * @param logHeads 要存储的模型对象列表
     */
    public void saveLogHeads(List<DbLogHead> logHeads) {
                                   PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logHead"));
//        sm.open();
        for (int i = 0; i < logHeads.size(); i++) {
            DbLogHead logHead = logHeads.get(i);
            sm.setRowKey(logHead.getRowKey());
            sm.put(DbLogHead.cl_name, logHead.getName());
            sm.put(DbLogHead.cl_location, logHead.getLocation());
            sm.put(DbLogHead.cl_chinese, logHead.getChinese());
        }
//        sm.close();
    }

    public List<DbLogHead> getLogHeads(final String columnName, final String value) {
        List<DbLogHead> result = new LinkedList<DbLogHead>();
                                PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logHead"));
//        sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String name = null, location = null, chinese = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbLogHead.cl_name)) {
                        name = columnsValue;
                    } else if (columnsName.equals(DbLogHead.cl_location)) {
                        location = columnsValue;
                    } else if (columnsName.equals(DbLogHead.cl_chinese)) {
                        chinese = columnsValue;
                    }
                }
                DbLogHead pLogHead = new DbLogHead(rowKey, name, location, chinese);
                result.add(pLogHead);
            }
        } catch (Exception ex) {
            Logger.getLogger(DbLogHead.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }

    public void deleteLogHead(final String rowKey) {
                                  PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logHead"));
//        sm.open();
        sm.delete(rowKey);
//        sm.close();
    }

    public ArrayList<String> getAllLogHeadName() {
                                       PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logHead"));
//        sm.open();
        List<KeySlice> crs = sm.getAll();
        ArrayList<String> logHeadNameList = new ArrayList<String>();
        try {
            for (KeySlice ks : crs) {
                String name = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbLogHead.cl_name)) {
                        name = columnsValue;
                    }
                }
                if (!logHeadNameList.contains(name)) {
                    logHeadNameList.add(name);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(LogHeadHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return logHeadNameList;
    }

    public static Map<String, String> getLogHeadsLocationMap() {
                                      PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logHead"));
        Map<String, String> headlocation = new HashMap<String, String>();
        List<KeySlice> crs = sm.getAll();
        String name = null, location = null;
        try {
            for (KeySlice ks : crs) {
                name = location = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbLogHead.cl_name)) {
                        name = columnsValue;
                    } else if (columnsName.equals(DbLogHead.cl_location)) {
                        location = columnsValue;
                    }
                }
                headlocation.put(name, location);
            }
        } catch (Exception ex) {
            Logger.getLogger(DbLogHead.class.getName()).log(Level.SEVERE, null, ex);
        }
        return headlocation;
    }
}
